package sudoku;

public class Manager{
	
	Board bd;
	Sudoku_Question Question;
	int hint_no=3;
	int qnum; //for question number
	
	char [][] Bd_arr = new char[9][9]; //Board array
		
	Validator validator=new Validator(); //validator is used as a tool for checking
	
	//default constructor
	public Manager(){}
	
	//copy constructor for copying question object
	public Manager(Sudoku_Question question)
	{
		this.Question=question;
		
	}
	
	//Copy Question to Board
	public Manager(int difficulties, int question_no)
	{
		QuestionFactory Question_Generator = new QuestionFactory();
		
		bd = new Board (); 				//Create new board
		
		qnum=question_no;
		
		//choose the question set (1 easy, 2 medium, 3 hard)
		Question = Question_Generator.create_question(difficulties);
		
		Bd_arr = bd.getboard(); 		// get the board
		
		bd.boardcopy(Question.get_question(qnum)); //copy the question to board
	}

	
	public void surrender()
	{
		bd.boardcopy(Question.get_solution(qnum));
		print_board();
		
	}
	public boolean is_invalid_length(String input)
	{
		return validator.is_invalid_length(input);
	}
	public boolean is_invalid_range(String input)
	{
		return validator.is_invalid_range(input);
		
	}
	public boolean contain_nonzero_position(int xPos,int yPos)
	{
		return validator.contain_nonzero_position(xPos,yPos);
		
	}
	public boolean is_invalid_value(String input)
	{
		return validator.is_invalid_value(input);
	}
	public boolean validate_input(int x , int y , String input, char i, int display)
	{
		return validator.check_input(Question.get_question(qnum),bd.getboard() ,x,y,input,i,display);
		
	}
	
	public void set_board(int x , int y , char i) {		
		bd.setboard(y,x,i);
	};
		
	public void print_board() {			 
		bd.display();
	}

	public boolean hint_ava(){ // Check if the hint number >0
		if (hint_no>0)
			return true;
		else
			return false;
	
	}
	
	public void get_hint(){//Complete one sub-grip for user
		char c = ' ';
		int x=0,y=0;
		boolean found=false;
		boolean Error = false;
	
		while (!found && y<9){
			//Checking repeat or not
			if (Bd_arr[y][x]==' ')
			{		
				char [][] Sol_arr = Question.get_solution(qnum);
				c = Sol_arr[y][x];
				StringBuilder sb = new StringBuilder();
				sb.append("");
				sb.append(c);
			
				if(validate_input(x,y,sb.toString(),c,0))
					found = true;
				else{
					x++;
					Error = true;
				}
			}
			else	x++;			
			if(x==9)
				{x=0;y++;}									
		}			
		hint_no--;
		set_board(x,y,c);
		print_board();
		System.out.println("Hint Added X:"+x+" Y:"+y+" Number:"+c);
		System.out.println("Hints remain: "+hint_no);	
		
		if(Error)
			System.out.println("Please check again the previous input !");
	}
		
	public void show_step(int step) // show user used how many steps
	{
		System.out.println("Users have entered "+step+" steps");
		System.out.println();

	}

	public int count_step(int step) //increase the number of step by 1
	{
		step++;
		return step;
	}
	
	public Board getboard()   //return the board
	{	
		return bd;	
		
	}

	
	public boolean gamewin(){
		char [][] Sol_arr = Question.get_solution(qnum);
		for (int i=0;i<9;i++)
			for(int j=0;j<9;j++)
				if (Bd_arr[i][j]==' ' || Bd_arr[i][j]!=Sol_arr[i][j])
					return false;
		return true;
		
	}
	
	public boolean gameover(){
		char [][] Sol_arr = Question.get_solution(qnum);
		for (int i=0;i<9;i++)
			for(int j=0;j<9;j++)
				if (Bd_arr[i][j]==' ' || Bd_arr[i][j]==Sol_arr[i][j])
					return false;
		return true;
		
	}
	
}

